package main

import (
	"fmt"
	"net"
)

func main() {
	listen, err := net.Listen("tcp", "0.0.0.0:8888")
	defer listen.Close()
	if err != nil {
		fmt.Printf("listen error : %v \n", err)
		return
	}
	for {
		conn, connErr := listen.Accept()
		if connErr != nil {
			fmt.Printf("connect error : %v \n", connErr)
			return
		}
		showReceived(conn) //用go协程可以防止主线程阻塞
	}
}

func showReceived(conn net.Conn) {
	defer conn.Close()
	for {
		buf := make([]byte, 512)
		n, err := conn.Read(buf)
		if err != nil {
			return
		}
		receiveMsg := string(buf[0:n])
		fmt.Printf("received messages : %v \n", receiveMsg)
	}
}
